package cn.bx.bid.ctrl;

import cn.bx.bid.entity.*;
import cn.bx.bid.service.ProfessorService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.Date;

@RestController
public class ProfessorController {
    @Autowired
    ProfessorService professorService;

    @RequestMapping(value = "/professorSearch", produces = "application/json;charset=utf-8")
    public Page<Professor> paging(@RequestParam(name = "page", defaultValue = "1") int pageNo, @RequestParam(name = "limit", defaultValue = "10") int limit, @RequestParam(name = "name", required = false) String name, @RequestParam(name = "tele", required = false) String tele, @RequestParam(name = "idcard", required = false) String id) {
        Page<Professor> p = this.professorService.paging(pageNo, limit, name, tele, id);
        return p;
    }

    @RequestMapping(value = "/professorAdd", produces = "application/json;charset=utf-8")
    public AjaxResult professorAdd(@ModelAttribute("p") Professor p, HttpSession session ) {
        p.setCreateby(((User)session.getAttribute("USER_LOGIN")).getName());
        int ok = professorService.addprofessor(p);
        if (ok == 1)
            return AjaxResult.success();
        return AjaxResult.error();
    }
    @RequestMapping(value = "/professorGet", produces = "application/json;charset=utf-8")
    public AjaxResult professorGet(@RequestParam(name = "id") long id){
        Professor p = this.professorService.get(id);
        if(p==null)
            return AjaxResult.error();
        return AjaxResult.success(p);
    }
    @RequestMapping(value = "/historyAdd", produces = "application/json;charset=utf-8")
    public AjaxResult historyAdd(@ModelAttribute("w") WorkHistory w) {
        int r = this.professorService.addHistory(w);
        if(r==1)
            return AjaxResult.success();
        return AjaxResult.error();

    }
    @InitBinder
    public void initBinder(WebDataBinder binder) {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));
        // true:允许输入空值，false:不能为空值
    }

}
